﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using RPGOnline.Remoting;
using RPGOnline.Remoting.Entities;

namespace RPGOnline.Controls.Chat
{
    public partial class CreateChatGroup : Form
    {
        private readonly ChatManager m_chatManager;
        private readonly SessionContext m_sessionContext;

        public CreateChatGroup(ChatManager chatManager, SessionContext sessionContext)
        {
            m_chatManager = chatManager;
            m_sessionContext = sessionContext;

            InitializeComponent();

            Fill();
            tvUsers.ExpandAll();
        }

        public void Fill()
        {
            tvUsers.BeginUpdate();
            tvUsers.Nodes.Clear();

            foreach (ChatGroup chatGroup in m_sessionContext.Groups)
            {
                TreeNode chatGroupNode = tvUsers.Nodes.Add(chatGroup.Id.ToString(), chatGroup.Name);
                chatGroupNode.Tag = chatGroup;

                FillGroup(chatGroupNode, chatGroup);
            }

            tvUsers.EndUpdate();
        }

        private void FillGroup(TreeNode chatGroupNode, ChatGroup chatGroup)
        {
            foreach (User user in chatGroup.Users.Values)
            {
                TreeNode treeNode = chatGroupNode.Nodes.Add(user.Id.ToString(), user.Name);
                treeNode.Tag = user;
            }
        }

        private void bnCreate_Click(object sender, EventArgs e)
        {
            List<User> usersInMainChat = new List<User>();
            List<User> usersInOtherGroups = new List<User>();

            foreach (TreeNode groupNodes in tvUsers.Nodes)
            {
                if((groupNodes.Tag as ChatGroup).Id == Guid.Empty)
                {
                    PushSelectedUsersInList(groupNodes, usersInMainChat);
                }
                else
                {
                    PushSelectedUsersInList(groupNodes, usersInOtherGroups);
                }
            }

            if(usersInOtherGroups.Count != 0)
            {
                if (MessageBox.Show(this, string.Format("{0} of the selected users are in other groups, these will be moved to the new group!\r\nAre you sure you want to continue?", usersInOtherGroups.Count), "Multiple groups not allowed", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes)
                    return;
            }

            ChatGroup chatGroup = GroupManager.CreateGroup(tbGroupName.Text);
            m_chatManager.Notify.CreateGroup(chatGroup);
            usersInMainChat.ForEach(u => m_chatManager.Notify.MoveUserToGroup(u, chatGroup.Id));
            usersInOtherGroups.ForEach(u => m_chatManager.Notify.MoveUserToGroup(u, chatGroup.Id));

            DialogResult = DialogResult.OK;
        }

        private void PushSelectedUsersInList(TreeNode groupNode, List<User> usersList)
        {
            foreach (TreeNode node in groupNode.Nodes)
            {
                if (!node.Checked)
                    continue;

                usersList.Add(node.Tag as User);
            }
        }
    }
}
